<script lang="ts">
import { onMounted } from "vue";
import { defineComponent, ref } from "vue";
import { useRouter, useRoute } from "vue-router";
import { playerTime } from "../hooks/filters";
import { storeToRefs } from 'pinia';
import { useCounterStore } from '..//stores/home'
export default defineComponent({
    setup() {

        const store = useCounterStore()
        const { idarr, likearr, collectlikearr, num, commentlikearr, userInfoheard } = storeToRefs(store)
        // 作为 action 的 increment 可以直接解构
        const { increment, changnum, incrementfalse, unlikeFuns, likeFuns, collectFuns, getUserInfoFun, uncollectFuns, commentFuns, uncommentFuns } = store;

        let router: any = useRouter();
        let route: any = useRoute();
        const show = ref(false);

        let mySendNoteId = route.query.id
        let mySendNote: any = ref(null)

        let localarr = JSON.parse(window.localStorage.getItem("mynoteArr"));

        console.log("localarr=>", localarr)

        mySendNote.value = localarr.filter((item: any) => {
            return item.id == mySendNoteId

        })
        console.log("mySendNote=>", mySendNote.value)

        let bigpic: any = ref(null)
        let pic = (i: any) => {
            bigpic.value = mySendNote.value[0].images.filter((item: any, index: any) => {
                console.log(item)
                return index == i

            })
            console.log("bigpic", bigpic.value)
        }



        // 给自己发评论
        let local = JSON.parse(window.localStorage.getItem("tomyNote"))
        let mysentContent: any = ref(null)

        let mycomment: any = ref(local || [])//一个空数组接着自己的评论
        // 自创评论id
        let mycommentid = 1111

        let newmycomment: any = ref([])

        let sentmtcomment = () => {
            mycommentid += 1
            let currentTime = new Date().getTime()
            changnum(newmycomment.value.length + 1)
            console.log('11111111111', newmycomment.value.length);
            console.log('8888888', mycomment.value[mycomment.value.length - 1]);
            // console.log('8888888', mycomment.value.length);
            local = JSON.parse(window.localStorage.getItem("tomyNote"))

            mycomment = ref(local || [])
            if (mycomment.value.length == 0) {
                mycomment.value.push(
                    {
                        clicknoteId: mySendNoteId.value, //点击的手记id
                        id: 1111 , //评论id
                        contentText: mysentContent.value, //评论的内容
                        likeCount: 0,  //评论点赞数
                        createTimeUnix: currentTime,// 发评论的时间戳
                        userDetail: {
                            id: "10020974",  //发评论的人的id
                            // avatar_small: "https://cs.xinpianchang.com/uploadfile/user/2017/04/05/58e4be784ff29.jpeg", //发评论的人的头像
                            // nickname: "董北"  //发评论的人的昵称
                        }

                    }
                )
            } else {
                mycomment.value.push(
                    {
                        clicknoteId: mySendNoteId.value, //点击的手记id
                        id: (mycomment.value[0].id || 1111) + 1, //评论id
                        contentText: mysentContent.value, //评论的内容
                        likeCount: 0,  //评论点赞数
                        createTimeUnix: currentTime,// 发评论的时间戳
                        userDetail: {
                            id: "10020974",  //发评论的人的id
                            // avatar_small: "https://cs.xinpianchang.com/uploadfile/user/2017/04/05/58e4be784ff29.jpeg", //发评论的人的头像
                            // nickname: "董北"  //发评论的人的昵称
                        }

                    }
                )
            }


            mysentContent.value = ''

            newmycomment.value = mycomment.value.filter((item: any) => {
                return item.clicknoteId == mySendNoteId.value
            })

            // 发布时间 最新要在最上面
            newmycomment.value.sort(function (a: any, b: any) {
                return b.createTimeUnix - a.createTimeUnix
            })

            window.localStorage.setItem("tomyNote", JSON.stringify(mycomment.value))


            console.log('222222', newmycomment.value.length);


        }



        newmycomment.value = mycomment.value.filter((item: any) => {
            return item.clicknoteId == mySendNoteId.value
        })

        newmycomment.value.sort(function (a: any, b: any) {
            return b.createTimeUnix - a.createTimeUnix
        })

        // 删除我发的评论
        let delmycomment = (index: any) => {
            newmycomment.value.splice(index, 1);
            console.log('21111111111=>', newmycomment.value)
            window.localStorage.setItem("tomyNote", JSON.stringify(newmycomment.value))
            local = JSON.parse(window.localStorage.getItem("tomyNote"));
            console.log('3333333333333333', local);

        }

        // 判断是否是vip
        let localvip = JSON.parse(window.localStorage.getItem("vip"))

        let isvip: any = ref(localvip || {})












        onMounted(() => {
            local = JSON.parse(window.localStorage.getItem("tomyNote"))

            changnum(newmycomment.value.length)
            getUserInfoFun();
        })

        return {
            mySendNote,
            show,
            playerTime,
            pic,
            bigpic,
            sentmtcomment,
            mysentContent,
            newmycomment,
            idarr,
            incrementfalse,
            increment,
            delmycomment,
            commentFuns,
            uncommentFuns,
            commentlikearr,
            userInfoheard,
            isvip

        }
    }
})


</script>





<template>
    <div class="hand-detail">
        <!-- 顶部的头像等.... -->
        <div class="hand-detail-top">
            <span class="iconfont icon-xiangzuojiantou" @click="$router.back()"></span>

            <div class="top-center">
                <div class="center-name">
                    <img :src="userInfoheard.avatar">
                    <span>{{ userInfoheard.username }}</span>
                    <span class="iconfont icon-VIP1" style="color:red" v-if="isvip.Vip==1" ></span> 
                    <span class="iconfont icon-VIP1" style="color:gold" v-if="isvip.Vip==2"></span>
                    <!-- <span class="iconfont icon-jingxuan2"></span> -->
                </div>

                <!-- <p v-if="!idarr.includes(mySendNote[0].user.id)" @click="increment(mySendNote[0].user.id)">+ 关注</p>
                    <p v-else @click="incrementfalse(mySendNote[0].user.id)">已关注</p> -->

            </div>
            <span class="iconfont icon-fenxiang2"></span>
        </div>

        <div class="nulldiv" style="height:40px"></div>

        <!-- 内容和图片 -->
        <div class="hand-detail-content">
            <!-- 图片 -->
            <div class="img-box">
                <!-- <div class="imgs" v-for="(item,index) in Notesdetail.images" :key="index">
                            <img :src="item ">
                        </div> -->

                <!-- 遮罩层级 -->
                <van-button class="imgs" type="primary" v-for="(item, index) in mySendNote[0].images" :key="index"
                    @click="show = true; pic(index)">
                    <img :src="item.content">
                </van-button>

                <van-overlay :show="show" @click="show = false">
                    <div class="wrapper" @click.stop>
                        <div class="block" @click="show = false">
                            <img :src="bigpic[0].content">
                        </div>
                    </div>
                </van-overlay>
            </div>
            <!-- 文字 -->
            <div class="hand-detail-words">

                <!-- 标题 -->
                <h3>{{ mySendNote[0].title }}</h3>
                <!-- 内容 -->
                <p>
                    {{ mySendNote[0].content }}
                </p>

                <div class="hand-detail-words-tag" v-if="mySendNote.topics">
                    # {{ mySendNote.topics[0].name }}
                </div>
                <!-- 视频 -->
                <!-- <div class="hand-detail-video" v-if="Notesdetail.article.id">
                            <div class="hand-detail-video-pic" >
                                <img :src="getimage(Notesdetail.article.cover+'params50y50')">
                            </div>
                            <div class="hand-detail-video-pic-worlds">
                                <p class="video-name">{{Notesdetail.article.title}}</p>
                                <p class="video-author">@{{ Notesdetail.article.author.userinfo.username }}</p>
                            </div>
                        </div> -->
                <div class="time">
                    {{ playerTime(mySendNote[0].addtime) }} · {{ mySendNote[0].ip_location }}
                </div>

            </div>
        </div>

        <!-- 评论 -->
        <div class="hande-detail-comment">
            <div class="comment-top">
                共 {{ newmycomment.length }} 条评论
            </div>
            <div class="send-comment">
                <img :src="userInfoheard.avatar">
                <input type="text" placeholder="说说你的看法吧~" v-model="mysentContent" @keyup.enter="sentmtcomment">

            </div>

            <div class="others-comment-box" v-if="newmycomment.length">


                <!-- <div class="others-comment" v-for="(item,index) in newmycomment" :key="index" > -->
                <!-- 用户头像 -->
                <!-- <div class="comment-pic" v-if="item.userDetail">
                             <img :src="item.userDetail.avatar_small">
                        </div>
               
                        <div class="comment-content" v-if="item.userDetail">
                            <div class="nickname" >
                                <p class="others-name">{{item.userDetail.nickname}} <span class="iconfont icon-VIP1"></span></p>
                                <p class="others-worlds">{{item.contentText}}</p>
                            </div>
                            <div>
                                <em style="margin-right: 3px; font-size: 12px;">{{ item.likeCount }}</em>
                                <span class="iconfont icon-dianzan1"></span>
                            </div>
                    
                        </div> -->
                <!-- </div> -->

                <van-swipe-cell class="others-comment" v-for="(item, index) in newmycomment" :key="index">
                    <!-- <div class="others-comment"  v-for="(item,index) in newCommentList" :key="index"> -->
                    <!-- 用户头像 -->
                    <div class="comment-pic" v-if="item.userDetail">
                        <img :src="userInfoheard.avatar">
                    </div>

                    <div class="comment-content" v-if="item.userDetail">
                        <div class="nickname">
                            <p class="others-name">{{ userInfoheard.username }}
                                <!-- <span class="iconfont icon-VIP1"></span> -->
                            </p>
                            <p class="others-worlds">{{ item.contentText }}</p>
                        </div>
                        <!-- <div>
                                <em style="margin-right: 3px; font-size: 12px;">{{ item.likeCount }}</em>
                                <span class="iconfont icon-dianzan1"></span>
                            </div> -->
                        <!-- 点赞不红 -->
                        <div v-if="!commentlikearr.includes(item.id)">
                            <em style="margin-right: 3px; font-size: 12px;">{{ item.likeCount }}</em>
                            <span class="iconfont icon-dianzan" @click.stop="commentFuns(item.id)"></span>
                        </div>
                        <!-- 点了赞变红了 -->
                        <div v-else>
                            <em style="margin-right: 3px; font-size: 12px;">{{ item.likeCount + 1 }}</em>
                            <span class="iconfont icon-dianzan_kuai" style="color:red"
                                @click.stop="uncommentFuns(item.id)"></span>
                        </div>


                    </div>
                    <!-- </div> -->
                    <template #right>
                        <van-button square type="danger" text="删除" @click="delmycomment(index)" />
                    </template>
                </van-swipe-cell>
            </div>


            <div class="null-comment" v-else>
                <p>暂无评论,说说你的看法吧</p>
            </div>


        </div>







    </div>
</template>


    


<style lang="scss">
.van-overlay {
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: rgba(0, 0, 0, 0.8) !important;
}

.wrapper {
    display: flex;
    align-items: center;
    justify-content: center;
    // height: 100%;
}

.block {
    // width: 120px;
    // height: 120px;
    background-color: #fff;
    // position: relative;
    height: 100%;
    display: flex;
    align-items: center;

    img {
        // position: absolute;
        // top: 135px;
        // left: -188px;
        width: 375px;
        height: auto;
    }
}

* {
    padding: 0;
    margin: 0;

}

.hand-detail {
    .hand-detail-top {
        padding-top: 10px;
        width: 100%;
        height: 42px;
        padding-left: 16px;
        padding-right: 16px;
        box-sizing: border-box;
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding-bottom: 10px;
        border-bottom: 2px solid #f6f6f6;
        position: fixed;
        z-index: 999;
        background-color: #ffffff;

        .icon-xiangzuojiantou {
            font-size: 20px;
            font-weight: bolder;
            width: 3%;
        }

        .icon-fenxiang2 {
            font-size: 25px;
            width: 5%;
            margin-left: 5%;

        }

        .top-center {
            flex: 1;
            display: flex;
            justify-content: space-between;
            margin-left: 6%;
            align-items: center;

            .center-name {
                display: flex;
                align-items: center;

                img {
                    width: 32px;
                    height: 32px;
                    border-radius: 50%;
                    margin-right: 10px;
                }
            }

            p {
                width: 65px;
                height: 25px;
                border-radius: 20px;
                color: #cd4453;
                border: 2px solid #cd4453;
                line-height: 27px;
                text-align: center;
                font-size: 12px;
            }
        }
    }

    .hand-detail-content {
        padding-left: 16px;
        padding-right: 16px;
        box-sizing: border-box;
        padding-top: 15px;
        border-bottom: 2px solid #f7f7f7;

        .img-box {
            display: flex;
            flex-wrap: wrap;
            // justify-content: space-between;
            padding-bottom: 20px;

            // margin-bottom: 20px;
            .imgs {
                width: 110px;
                height: 110px;
                border-radius: 3px;
                overflow: hidden;
                margin-bottom: 10px;
                background-color: #aba9b1;
                border: none;
                padding: 0px;
                margin-right: 5px;

                &:nth-last-of-type(3),
                &:nth-last-of-type(6),
                &:nth-last-of-type(9) {
                    margin-right: 0px;
                }

                .van-button__content {
                    width: 110px;
                    height: 110px;

                    .van-button__text {
                        width: 110px;
                        height: 110px;
                    }
                }

                img {
                    width: 100%;
                    height: 100%;
                }
            }
        }

        .hand-detail-words {
            // margin-top: 20px;

            padding-bottom: 16px;

            h3 {
                font-weight: bold;
                margin-bottom: 10px;
            }

            .hand-detail-words-tag {
                color: #466eb6;
                margin-top: 5px;
            }

            .hand-detail-video {
                display: flex;

                .hand-detail-video-pic {
                    width: 160px;
                    height: 92px;
                    border-radius: 5px 0px 0px 5px;
                    overflow: hidden;

                    img {
                        width: 100%;
                        height: 100%;
                    }
                }

                .hand-detail-video-pic-worlds {
                    width: 184px;
                    height: 92px;
                    border-radius: 0px 5px 5px 0px;
                    background-color: #f3f4f6;
                    padding: 14px;
                    box-sizing: border-box;

                    .video-name {
                        font-size: 13px;
                        -webkit-line-clamp: 2;
                        display: -webkit-box;
                        -webkit-box-orient: vertical;
                        overflow: hidden;
                        text-overflow: ellipsis;
                    }

                    .video-author {
                        margin-top: 12px;
                        color: #95969b;
                        font-size: 12px;
                    }


                }
            }

            .time {
                color: #aba9b1;
                font-size: 11px;
                margin-top: 10px;
            }
        }
    }

    .hande-detail-comment {

        width: 100%;
        height: 500px;
        padding-left: 16px;
        box-sizing: border-box;

        .comment-top {
            font-size: 14;
            color: #525251;
            margin-top: 18px;

        }

        .send-comment {
            display: flex;
            padding-right: 16px;
            ;
            margin-top: 20px;

            img {
                width: 35px;
                height: 35px;
                border-radius: 50%;
                margin-right: 11px;
            }

            input {
                flex: 1;
                outline: none;
                border: none;
                height: 37px;
                background-color: #f3f4f6;
                border-radius: 20px;
                text-indent: 20px;
                font-size: 14px;

            }
        }

        .others-comment-box {
            .others-comment {




                .van-swipe-cell__wrapper {
                    width: 100%;
                    display: flex;

                    .van-button {
                        height: 100%;
                    }
                }





                display: flex;
                margin-top: 20px;

                .comment-pic {
                    img {
                        width: 35px;
                        height: 35px;
                        border-radius: 50%;
                        margin-right: 10px;
                    }
                }

                .comment-content {
                    display: flex;
                    flex: 1;
                    border-bottom: 2px solid #f7f7f7;
                    justify-content: space-between;
                    padding-bottom: 16px;

                    .nickname {
                        .others-name {
                            color: #98979d;
                            font-size: 13px;

                            .icon-VIP1 {
                                color: red;
                            }
                        }

                        .others-worlds {
                            margin-top: 9px;
                        }

                    }

                    span {
                        margin-right: 16px;
                    }
                }
            }

        }

        .null-comment {
            margin-top: 20px;
            text-align: center;

            p {
                text-align: center;
                font-size: 14px;
                color: #98979d;
            }
        }

    }
}
</style>